<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>钉钉扫码</title>
		<script src="https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script>
		<script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
	</head>
	<body>
		<div class="text-center">
		    <div id="login_container"></div>
		    <!-- <button type="button" class="btn btn-primary" id="JumpToLogin">账号密码登录</button> -->
		</div>
		
		<script type="text/javascript">
			/*
			* 解释一下goto参数，参考以下例子：
			* var url = encodeURIComponent('http://localhost.me/index.php?test=1&aa=2');
			* var goto = encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=AppKey&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+url)
			*/
		   var login_url = "http://ztz0570.vaiwan.com/api/dingtalk/login";
		   var goto_url = encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingladia65qjlnnqmch&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+login_url)
			var obj = DDLogin({
			     id:"login_container",//这里需要你在自己的页面定义一个HTML标签并设置id，例如<div id="login_container"></div>或<span id="login_container"></span>
			     goto: goto_url, //请参考注释里的方式
			     style: "border:none;background-color:#FFFFFF;",
			     width : "365",
			     height: "400"
			 });
			 var handleMessage = function (event) {
			   var origin = event.origin;
			   console.log("origin", event.origin);
			   if( origin == "https://login.dingtalk.com" ) { //判断是否来自ddLogin扫码事件。
			     var loginTmpCode = event.data; 
			     //获取到loginTmpCode后就可以在这里构造跳转链接进行跳转了
			     
			     console.log("loginTmpCode", loginTmpCode);
				 if(loginTmpCode){
					  let url =
					             "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingladia65qjlnnqmch&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=" +
					             login_url +
					             "&loginTmpCode=" +
					             loginTmpCode;
					           window.location.href = url;  
				 }
			   }
			 };
			 // if (typeof window.addEventListener != 'undefined') {
			 //     window.addEventListener('message', handleMessage, false);
			 // } else if (typeof window.attachEvent != 'undefined') {
			 //     window.attachEvent('onmessage', handleMessage);
			 // }
			 
			//  $("#JumpToLogin").click(function(){
			// let jump_url = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingladia65qjlnnqmch&response_type=code&scope=snsapi_login&state=STATE&redirect_uri="+encodeURIComponent(login_url);
			//  window.location.href = jump_url;
			//          });
		</script>
	</body>
</html>
